package com.sth.test2;

import com.sth.util.MpCodeGet;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author: sitianhong
 * @Description:
 * @Since: create in 2021/11/30 0030 8:47
 */
public class MyJComponent2 {
    public static JComponent getTest2Pannel() {
        JPanel panel = new JPanel(false);
        panel.setLayout(null);    //面板使用网格4X4布局
        panel.setBounds(400, 400, 400, 400);
        JLabel label = new JLabel("没做呢：");
        label.setBackground(Color.red);
        label.setBounds(50, 10, 120, 100);
        JLabel label2 = new JLabel("数据库名称：");
        label2.setBounds(50, 40, 120, 100);


        JLabel label3 = new JLabel("数据库用户名：");
        label3.setBounds(50, 70, 120, 100);

        JLabel label4 = new JLabel("数据库密码：");
        label4.setBounds(50, 100, 120, 100);

        JLabel label5 = new JLabel("数据库表名：");
        label5.setBounds(50, 130, 120, 100);

        JTextField outPutDirField = new JTextField(25);
        outPutDirField.setBounds(150, 50, 450, 25);
        JTextField dbNameField = new JTextField(25);// 处理后的文件名称
        dbNameField.setBounds(150, 80, 450, 25);
        JTextField dbUsernameField = new JTextField();
        dbUsernameField.setBounds(150, 110, 450, 25);
        dbUsernameField.setText("root");
        JTextField dbPasswordField = new JTextField();
        dbPasswordField.setText("admin");
        dbPasswordField.setBounds(150, 140, 450, 25);
        JTextField tableNamesField = new JTextField();
        tableNamesField.setBounds(150, 170, 450, 25);
        JButton button = new JButton("浏览文件");
        button.setBounds(605, 50, 100, 25);
        panel.add(label);
        panel.add(label4);
        panel.add(label5);
        panel.add(outPutDirField);
        panel.add(dbPasswordField);
        panel.add(tableNamesField);
        panel.add(button);
        panel.add(label2);
        panel.add(dbNameField);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fc = new JFileChooser("E:\\");
                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int val = fc.showOpenDialog(null);    //文件打开对话框
                if (val == fc.APPROVE_OPTION) {
                    //正常选择文件
                    outPutDirField.setText(fc.getSelectedFile().toString());
                } else {
                    //未正常选择文件，如选择取消按钮
                    outPutDirField.setText("请选择文件");
                }

            }
        });
        JButton button2 = new JButton("开始处理");
        button2.setBounds(605, 170, 100, 25);
        panel.add(button2);
        panel.add(label3);
        panel.add(dbUsernameField);
        button2.addActionListener(new ActionListener() {
            @SneakyThrows
            public void actionPerformed(ActionEvent arg0) {
                if (StringUtils.isNotBlank(outPutDirField.getText()) && StringUtils.isNotBlank(dbNameField.getText())) {
                    MpCodeGet.doGet(outPutDirField.getText(), dbNameField.getText(), dbUsernameField.getText(), dbPasswordField.getText(), tableNamesField.getText().split(","));
                    JOptionPane.showMessageDialog(panel, "已经生成!", "提示", 1);
                } else {
                    JOptionPane.showMessageDialog(panel, "未选择文件或命名文件!", "提示", 1);
                }

            }
        });
        return panel;
    }
}
